import Vue from 'vue'
import Router from 'vue-router'
import BaseLayout from '@/views/Layout/BaseLayout'
import AdminLayout from '@/views/Layout/AdminLayout'
import { storage, pathConfig } from "@/config/configuration.js";
import * as localStorage from '@/utils/localStorageUtil';
import * as userApi from '@/api/admin/userApi';

Vue.use(Router)

const router = new Router({
  mode: 'history',
  routes: [
    {
      name:'frontDesk',
      path: '/',
      component: BaseLayout,
      children:[
        {
          name: '首页',
          path: '/',
          component: () => import('@/views/index/index.vue'),
        },
        {
          name: '联系我们',
          path: '/ContactUs',
          meta:{ lang:"contact.title",},
          component: () => import('@/views/contactUs/ContactUs.vue'),
        },
        {
          name: '关于我们',
          path: '/AboutUs',
          meta:{ lang:"about.title",},
          component: () => import('@/views/about/index.vue'),
        },
        {
          name: '新闻中心',
          path: '/News',
          meta:{ lang:"new.title"},
          component: () => import('@/views/new/list.vue'),
        },
        {
          name: '文章',
          path: '/News/Article',
          meta:{ lang:"new.title",bread:""},
          component: () => import('@/views/new/article.vue'),
        },
        {
          name: '产品列表',
          path: '/Product',
          meta:{ lang:"product.title"},
          component: () => import('@/views/product/list.vue'),
        },
        {
          name: '产品详情',
          path: '/Product/ProductDetails',
          meta:{ lang:"product.title"},
          component: () => import('@/views/product/productDetails.vue'),
        },
      ]
    },
    {
      name: '后台管理-用户登录',
      path: '/management/User/Login',
      component: () => import('@/views/admin/user/login.vue'),
    },
    {
      path: '/management',
      name:"后台管理",
      component: AdminLayout,
      redirect: "/management/home/CarouselList",
      children:[
        {
          name: '首页',
          path: '/management/home/CarouselList',
          component: () => import('@/views/admin/home/carouselList.vue'),
          meta: { requiresAuth: true }
        },
        {
          name: '轮播图表单',
          path: '/management/home/CarouselForm',
          component: () => import('@/views/admin/home/carouselForm.vue'),
          meta: { requiresAuth: true }
        },
        {
          name: '联系我们',
          path: '/management/ContactUs',
          component: () => import('@/views/admin/contactUs/index.vue'),
          meta: { requiresAuth: true }
        },
        {
          name: '关于我们',
          path: '/management/AboutUs',
          component: () => import('@/views/admin/aboutUs/index.vue'),
          meta: { requiresAuth: true }
        },
        {
          name: '新闻中心',
          path: '/management/Artcie',
          component: () => import('@/views/admin/artcie/list.vue'),
          meta: { requiresAuth: true }
        },
        {
          name: '新闻中心表单',
          path: '/management/Artcie/Form',
          component: () => import('@/views/admin/artcie/form.vue'),
          meta: { requiresAuth: true }
        },
        {
          name: '产品列表',
          path: '/management/Product/List',
          component: () => import('@/views/admin/product/list.vue'),
          meta: { requiresAuth: true }
        },
        {
          name: '产品列表表单',
          path: '/management/Product/Form',
          component: () => import('@/views/admin/product/form.vue'),
          meta: { requiresAuth: true }
        },
        {
          name: '产品分类',
          path: '/management/Product/categorie',
          component: () => import('@/views/admin/product/categorie.vue'),
          meta: { requiresAuth: true }
        },
        {
          name: '初始设置',
          path: '/management/web/init',
          component: () => import('@/views/admin/web/init.vue'),
          meta: { requiresAuth: true }
        }
      ]
    }
  ]
});

router.beforeEach((to, from, next) => {
  const isAuthenticated = localStorage.getLocalStorage(storage.JWT) // 检查用户是否已登录
  if (to.matched.some(record => record.meta.requiresAuth) && !isAuthenticated) { // 如果用户未登录且需要登录才能访问该页面
    next({ path: pathConfig.login }) 
  } 
  else {
    if(to.matched.some(record => record.meta.requiresAuth)){
    //验证jwt是否过期
    userApi.authentication()
      .then(res=>{
        if(res.code == 200){
          next();
        }
        else{
          //清除本地缓存
          next({ path: pathConfig.login }) 
        }
      })
      .catch(err=>{
        window.location.reload();
      })
    }
    else{
      next();
    }
  }
})

export default router

